package com.erp.activity

import android.app.Activity
import android.content.Intent
import android.util.Log
import android.view.View
import android.widget.AdapterView
import androidx.appcompat.app.AlertDialog
import com.erp.activityIKP.IXiaoZuKP
import com.erp.activityKP.XiaoZuKP
import com.erp.activityKView.XIaoZuKView
import com.erp.myAdapter.XZXiaoZuKAdapter
import com.erp.bean.XiaoZuKBean
import com.erp.R
import com.erp.util.Myutil
import kotlinx.android.synthetic.main.activity_xiaozu.*
import kotlinx.android.synthetic.main.header_knoya.view.*

/**
 * Created by Administrator on 2017/11/9/009.
 * 作者：刘洋
 */
class XiaoZuKActivity : BaseActivity(), XIaoZuKView, AdapterView.OnItemClickListener, View.OnClickListener {
    lateinit var xiaoZuKp : XiaoZuKP
    lateinit var xiaozuList: MutableList<XiaoZuKBean>
    lateinit var adapter : XZXiaoZuKAdapter
    lateinit var updialog: AlertDialog
    private val a = "XiaoZuKActivity"
    override fun getLayoutId(): Int {
        activityname=a;
        return R.layout.activity_xiaozu
    }

    override fun initCreate() {
        xiaoZuKp= IXiaoZuKP(this,this)
        xiaozuList=  ArrayList()
        adapter= XZXiaoZuKAdapter(xiaozuList, this)
        XiaoZuLv.adapter=adapter
        XiaoZuLv.onItemClickListener=this
        /**允许上拉*/
        ShuaXingPull.setheader(true)
        /**上拉加载*/
        ShuaXingPull.setOnFooterRefreshListener { xiaoZuKp.addXiaoZuShuJu() }
        /**下拉刷新*/
        ShuaXingPull.setOnHeaderRefreshListener { xiaoZuKp.getXiaoZuShuJu() }
        updialog = Myutil.showupdatedialog(this, "正在刷新", false)
        xiaoZuKp.getXiaoZuShuJu()
        XiaoZuHeader.iv_header_left.visibility=View.VISIBLE
        XiaoZuHeader.tv_header_title.text="选择小组"
        XiaoZuHeader.iv_header_left.setOnClickListener(this)
        QingKongIv.setOnClickListener(this)
        SouSuoIv.setOnClickListener(this)
        XiaoZuHeader.iv_header_left.isFocusable=true
        XiaoZuHeader.iv_header_left.isFocusableInTouchMode=true
        XiaoZuHeader.iv_header_left.requestFocus()
        XiaoZuHeader.iv_header_left.requestFocusFromTouch()
    }
    /**获取小组数据*/
    override fun xiaoZuShuJuOk(xiaoZuBeans: MutableList<XiaoZuKBean>) {
        xiaozuList.clear()
        for(xiaoZuKBean in xiaoZuBeans){
            xiaozuList.add(xiaoZuKBean)
        }
        adapter.notifyDataSetChanged()
        updialog.dismiss()
        ShuaXingPull.onHeaderRefreshComplete()
    }
    /**小组数据网络访问失败*/
    override fun xiaoZShuJuNo(error: String) {
        ShuaXingPull.onHeaderRefreshComplete()
        updialog.dismiss()
    }
    /**加载第二批小组数据失败*/
    override fun addxiaoZShuJuNo(error: String) = ShuaXingPull.onFooterRefreshComplete()
    /**加载第二批小组数据*/
    override fun addXiaoZuShuJuOk(xiaoZuBeans:  MutableList<XiaoZuKBean>) {
        for(xiaoZuKBean in xiaoZuBeans){
            xiaozuList.add(xiaoZuKBean)
        }
        adapter.notifyDataSetChanged()
        ShuaXingPull.onFooterRefreshComplete()
    }
    /**选择小组成功*/
    override fun XuanZeHttpXiaoZuOk(s: String) {
        var intent=Intent()
        intent.putExtra("Ok","Ok")
        setResult(Activity.RESULT_OK,intent)
        updialog.dismiss()
        finish()
    }
    /**选择小组失败*/
    override fun XuanZuHttoXiaozuNo(error: String) {
        Myutil.MyTost(Myutil.decodeUnicode(error))
        updialog.dismiss()
    }
    override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
        Log.d("TAAAG","onItemClick="+position)
        updialog = Myutil.showupdatedialog(this@XiaoZuKActivity, "正在更改小组", false)
        xiaoZuKp.setXiaoZU(xiaozuList[position])
    }
    override fun onClick(v: View?) {
        when(v){
            XiaoZuHeader.iv_header_left -> {
                finish()
            }
            QingKongIv -> {
                xiaoZuKp.cleanSouSuo()
                NameEt.setText("")
                GongHaoEt.setText("")
                ZuHaoEt.setText("")
                updialog = Myutil.showupdatedialog(this@XiaoZuKActivity, "正在刷新", false)
                xiaoZuKp.getXiaoZuShuJu()
            }
            SouSuoIv -> {
                xiaoZuKp.setSuoSuo(NameEt.text.toString(),GongHaoEt.text.toString(),ZuHaoEt.text.toString())
                updialog = Myutil.showupdatedialog(this@XiaoZuKActivity, "正在筛选", false)
                xiaoZuKp.getXiaoZuShuJu()
            }
        }
    }

}
